package cn.git.agent.loader;

/**
 * @description: 用于加载插件当中的拦截器interceptor
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-12-27
 */
public class InterceptorInstanceLoader {

    /**
     * 加载拦截器
     * @param interceptorClassName 拦截器全类目
     * @param targetClassLoader 自定义类加载器,要想插件拦截器中能够访问到被拦截的类，需要是同一个类加载器或者子类类加载器
     *                    eg : classA被拦截，ClassLoaderA加载，那么拦截器也必须是ClassLoaderA加载，否则无法访问到classA
     * @param <T> ConstructorMethodAroundInterceptor, MethodAroundInterceptor,
     * @return
     */
    public static <T> T load(String interceptorClassName, ClassLoader targetClassLoader) throws ClassNotFoundException,
            IllegalAccessException, InstantiationException {
        if (targetClassLoader == null) {
            targetClassLoader = InterceptorInstanceLoader.class.getClassLoader();
        }
        AgentClassLoader agentClassLoader = new AgentClassLoader(targetClassLoader);
        Object o = Class.forName(interceptorClassName, true, agentClassLoader).newInstance();
        return (T) o;
    }

}
